package com.wejoy.video.middle.station.admin.app;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.wejoy.video.middle.station.admin.filter.LoginPermissionsFilter;
import com.wejoy.video.middle.station.admin.filter.SysLogFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.math.BigInteger;
import java.util.List;

/**
 * @author: admin
 * @time: 2022/9/16 11:20
 */
@Configuration
public class InterceptorConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private LoginPermissionsFilter loginPermissionsFilter;
    @Autowired
    private SysLogFilter sysLogFilter;
    @Value("${localFilePath}")
    private String localFilePath;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sysLogFilter).addPathPatterns("/**/*");
        registry.addInterceptor(loginPermissionsFilter).addPathPatterns("/**/*");
        super.addInterceptors(registry);
    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);

        // 反序列化时忽略多余字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 注册
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(0, jackson2HttpMessageConverter);
        super.extendMessageConverters(converters);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //                          访问路径                         图片本地地址
        registry.addResourceHandler("/files/**").addResourceLocations("file:"+localFilePath);
        super.addResourceHandlers(registry);
    }
}
